package com.kll.tv.views

import android.content.Intent
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.kll.carolinehmibiz.presentation.listener.MenuDetailsProtocol
import com.kll.carolinehmibiz.presentation.presenter.MenuDetailsPresenter
import com.kll.tv.base.views.BaseActivity
import com.kll.tv.databinding.ActivityExternalLinksDetailsBinding


/**
 * 外部链接详情页
 */
class ExternalLinksDetailsActivity: BaseActivity<MenuDetailsPresenter>(), MenuDetailsProtocol.IView {
    private lateinit var binding: ActivityExternalLinksDetailsBinding
    private var url = ""

    override fun initViewBinding(): View {
        binding = ActivityExternalLinksDetailsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun createPresenter(): MenuDetailsPresenter {
        return MenuDetailsPresenter(this, this)
    }

    override fun init() {
        if (intent.getStringExtra("url")!!.indexOf("http") == -1) {
            url = "http://app.huaxia-health.com/static/h5/index.html#/pages/404/404"
        } else {
            url = intent.getStringExtra("url")!!
        }

        binding.tvTitle.text = intent.getStringExtra("menuTitle")

        if (intent.getStringExtra("menuTitle") == "隐私政策") {
            binding.ivBackCircular.visibility = View.GONE
        }

        binding.wvContent.loadUrl(url)
        binding.wvContent.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                // view?.loadUrl(path)
                return super.shouldOverrideUrlLoading(view, url)
            }
        }
        binding.wvContent.settings.textZoom = 100
        binding.wvContent.settings.useWideViewPort = true // 扩大比例的缩放
        binding.wvContent.settings.loadWithOverviewMode = true // 自适应屏幕
        binding.wvContent.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN // 自适应屏幕
        binding.wvContent.settings.setSupportZoom(true)
        binding.wvContent.settings.javaScriptEnabled = true // 启用js
        binding.wvContent.settings.domStorageEnabled = true // 启用domStorage
        binding.wvContent.settings.setAppCacheEnabled(true) // 启用本地缓存

        initListener()
    }

    private fun initListener() {
        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.ivBackCircular.setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }

}